package edu.gyc.producer.consumer;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

class MyResource{
   private volatile boolean FLAG=true;
    //private volatile boolean FLAG=true;
    private AtomicInteger atomicInteger = new AtomicInteger();
    private  static Integer count=0;
    BlockingQueue<String > blockingQueue=null;

    public MyResource(BlockingQueue<String> blockingQueue) {
        this.blockingQueue=blockingQueue;
        System.out.println(blockingQueue.getClass().getName());
    }

    public void put()throws Exception{
        String data=null;
        boolean retvalue;
        while (FLAG){
         data = String.valueOf(atomicInteger.incrementAndGet());
            /*
            synchronized (count){
                ++count;
            }
            data = String.valueOf(count);
             */
           retvalue= blockingQueue.offer(data, 1L, TimeUnit.SECONDS);
            if (retvalue) {
                System.out.println(Thread.currentThread().getName()+"\t插入队列"+data+"成功");
            }else {
                System.out.println(Thread.currentThread().getName()+"\t插入队列"+data+"失败");
            }
       Thread.sleep(50);
        }
        System.out.println(Thread.currentThread().getName()+"\t 大老板叫停了，表示FLAG=flase,生产动作结束");
    }

    public void get()throws Exception{
        String result=null;
        while (FLAG) {
            Thread.sleep(100);
          result=  blockingQueue.poll(1l, TimeUnit.SECONDS);
            if (null == result || result.equalsIgnoreCase("")) {
                FLAG=false;
                System.out.println(Thread.currentThread().getName()+"\t超过两秒，没有取到数据，消费退出。"+" 队列情况："+blockingQueue.toString());
                System.out.println();
                return;
            }
            System.out.println(Thread.currentThread().getName() + "\t 消费队列get " + result + "成功"+blockingQueue.toString());
        }
    }

    public void stop() {
        FLAG=false;
    }
}
public class ProduceConsumer_BlockQueueDemo {

    public static void main(String[] args) {
        MyResource myResource = new MyResource(new ArrayBlockingQueue<>(10));
        new Thread(()->{
            System.out.println(Thread.currentThread().getName()+"\t生产线程启动");

                try {
                    myResource.put();
                } catch (Exception e) {
                    e.printStackTrace();
                }

        },"生产1").start();
        new Thread(()->{
            System.out.println(Thread.currentThread().getName()+"\t生产线程启动");

            try {
                myResource.put();
            } catch (Exception e) {
                e.printStackTrace();
            }

        },"生产2").start();
        new Thread(()->{
            System.out.println(Thread.currentThread().getName()+"\t生产线程启动");

            try {
                myResource.get();
            } catch (Exception e) {
                e.printStackTrace();
            }

        },"消费1").start();
        new Thread(()->{
            System.out.println(Thread.currentThread().getName()+"\t生产线程启动");

            try {
                myResource.get();
            } catch (Exception e) {
                e.printStackTrace();
            }

        },"消费2").start();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println();
        System.out.println("5秒时间到，大老板叫停线程，活动结束");
        myResource.stop();
    }
}
